package com.csg.ses.service.ses.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csg.ses.common.utils.CommonUtil;
import com.csg.ses.common.utils.MybatisPlusUtil;
import com.csg.ses.dao.ses.SysOrganizationDao;
import com.csg.ses.entity.ses.SysOrganizationEntity;
import com.csg.ses.service.ses.SysOrganizationService;
import com.csg.ses.vo.common.OrgVo;
import com.csg.ses.vo.common.Result;
import com.csg.ses.vo.common.ResultStatus;
import com.csg.ses.vo.login.UserContext;
import com.csg.ses.vo.login.UserView;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 组织
 * @author wangjun
 * @date 2020/11/16
 */
@Service
@RequiredArgsConstructor
public class SysOrganizationServiceImpl extends ServiceImpl<SysOrganizationDao, SysOrganizationEntity> implements SysOrganizationService {
    private final SysOrganizationDao sysOrganizationDao;

    @Override
    public Result retrieve(SysOrganizationEntity org, Long current, Long size) {
        try {
            Page<SysOrganizationEntity> page = MybatisPlusUtil.initPage(current, size, true);
            QueryWrapper<SysOrganizationEntity> qw = new QueryWrapper();
            if (!CommonUtil.isEmpty(org.getOrgCode())) {
                qw.eq("org_code", org.getOrgCode());
            }
            if (!CommonUtil.isEmpty(org.getOrgName())) {
                qw.like("org_name", org.getOrgName());
            }
            qw.orderByAsc("org_code");
            return new Result(sysOrganizationDao.retrieve(page, qw), ResultStatus.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultStatus.FAILED);
        }
    }

    @Override
    public SysOrganizationEntity getUserLoginOrg(String orgId) {
        if (orgId != null) {
            QueryWrapper<SysOrganizationEntity> queryWrapper = new QueryWrapper();
            queryWrapper.eq("org_id", orgId);
            return sysOrganizationDao.selectOne(queryWrapper);
        } else {
            return null;
        }
    }

    @Override
    public Result getById(String id) {
        if (!CommonUtil.isEmpty(id)) {
            return new Result(sysOrganizationDao.selectById(id), ResultStatus.SUCCESS);
        } else {
            throw new RuntimeException("id不能为空！");
        }
    }

    @Override
    public List<SysOrganizationEntity> getOrgList() {
        try {
            UserView userView = UserContext.getUserView();
            return sysOrganizationDao.getOrgList(userView.getOrgId());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public List<OrgVo> tree() {
        QueryWrapper<SysOrganizationEntity> qw = new QueryWrapper();
        qw.orderByAsc("org_code");
        List<SysOrganizationEntity> orgList = sysOrganizationDao.selectList(qw);
        List<OrgVo> ov = this.transToTree(orgList);
        return ov;
    }

    /**
     * 转树形结构
     * @param orgList
     * @return
     */
    private List<OrgVo> transToTree(List<SysOrganizationEntity> orgList) {
        List<OrgVo> result = new ArrayList<OrgVo>();
        for (SysOrganizationEntity org : orgList) {
            if ("0".equals(org.getParentOrgId())) {
                result.add(this.findChildren(org, orgList));
            }
        }
        return result;
    }

    /**
     * 递归查找下级
     * @param org
     * @param orgList
     * @return
     */
    private OrgVo findChildren(SysOrganizationEntity org, List<SysOrganizationEntity> orgList) {
        OrgVo ov = new OrgVo();
        ov.setOrgId(org.getOrgId());
        ov.setOrgCode(org.getOrgCode());
        ov.setOrgName(org.getOrgName());
        ov.setParentOrgId(org.getParentOrgId());
        ov.setStartDate(org.getStartDate());
        ov.setEndDate(org.getEndDate());

        for (SysOrganizationEntity oe : orgList) {
            if (org.getOrgId().equals(oe.getParentOrgId())) {
                if (ov.getChildren() == null) {
                    ov.setChildren(new ArrayList<SysOrganizationEntity>());
                }
                ov.getChildren().add(findChildren(oe, orgList));
            }
        }
        return ov;
    }
}